import {AxdrType} from "../asn1.axdr/AxdrType";
import {ReverseByteArrayInputStream} from "../ReverseByteArrayInputStream";
import {ReverseByteArrayOutputStream} from "../ReverseByteArrayOutputStream";
import {Buffer} from "buffer";
import {AxdrNull} from "../asn1.axdr/AxdrNull";
import {PasswordSecurity} from "./PasswordSecurity";
import {SymmetrySecurity} from "./SymmetrySecurity";
import {SignatureSecurity} from "./SignatureSecurity";
import {AxdrEnum} from "../asn1.axdr/AxdrEnum";


enum Choice {
    _ERR_NONE_SELECTED = -1,
    NULL_SECURITY = 0,
    PASSWORD_SECURITY = 1,
    SYMMETRY_SECURITY = 2,
    SIGNATURE_SECURITY = 3,
}


export class ConnectMechanismInfo implements AxdrType{

    dataCode : Buffer | null = null;
    choice : Choice = Choice._ERR_NONE_SELECTED;
    nullSecurity : AxdrNull | null = null;
    passwordSecurity : PasswordSecurity | null = null;
    symmetrySecurity : SymmetrySecurity | null = null;
    signatureSecurity : SignatureSecurity | null = null;

    constructor() {
    }

    set_dataCode(dataCode : Buffer){
        this.dataCode = dataCode;
    }

    setNullSecurity(nullSecurity : AxdrNull){
        this.resetChoices();
        this.choice = Choice.NULL_SECURITY;
        this.nullSecurity = nullSecurity;
    }

    setPasswordSecurity(passwordSecurity : PasswordSecurity){
        this.resetChoices();
        this.choice = Choice.PASSWORD_SECURITY;
        this.passwordSecurity = passwordSecurity;
    }

    setSymmetrySecurity(symmetrySecurity : SymmetrySecurity){
        this.resetChoices();
        this.choice = Choice.SYMMETRY_SECURITY;
        this.symmetrySecurity = symmetrySecurity;
    }

    setSignatureSecurity(signatureSecurity : SignatureSecurity){
        this.resetChoices();
        this.choice = Choice.SIGNATURE_SECURITY;
        this.signatureSecurity = signatureSecurity;
    }

    decode(input: ReverseByteArrayInputStream): number {
        let codeLength = 0;
        let choosen = new AxdrEnum();
        choosen.set_const();

        codeLength += choosen.decode(input);
        this.resetChoices();
        this.choice = choosen.getValue();

        if (this.choice == Choice.NULL_SECURITY) {
            this.nullSecurity = new AxdrNull();
            codeLength += this.nullSecurity.decode(input);
            return codeLength;
        }else if (this.choice == Choice.PASSWORD_SECURITY) {
            this.passwordSecurity = new PasswordSecurity();
            codeLength += this.passwordSecurity.decode(input);
            return codeLength;
        }else if (this.choice == Choice.SYMMETRY_SECURITY) {
            this.symmetrySecurity = new SymmetrySecurity();
            codeLength += this.symmetrySecurity.decode(input);
            return codeLength;
        }else if (this.choice == Choice.SIGNATURE_SECURITY) {
            this.signatureSecurity = new SignatureSecurity();
            codeLength += this.signatureSecurity.decode(input);
            return codeLength;
        }
        throw new Error("Error decoding AxdrChoice: Identifier matched to no item.");
    }

    encode(output: ReverseByteArrayOutputStream): number {
        if (this.dataCode != null) {
            for (let i = this.dataCode.length - 1; i >= 0; i--) {
                output.write(this.dataCode[i]);
            }
            return this.dataCode.length;

        }
        if (this.choice == Choice._ERR_NONE_SELECTED) {
            throw new Error("Error encoding AxdrChoice: No item in choice was selected.");
        }

        let codeLength = 0;

        if (this.choice == Choice.SIGNATURE_SECURITY) {
            if(this.signatureSecurity != null){
                codeLength += this.signatureSecurity.encode(output);
                let c = new AxdrEnum();
                c.set_min_max_val(3);
                codeLength += c.encode(output);
                return codeLength;
            }else{
                throw new Error("signatureSecurity is null");
            }
        }else if (this.choice == Choice.SYMMETRY_SECURITY) {
            if(this.symmetrySecurity != null){
                codeLength += this.symmetrySecurity.encode(output);
                let c = new AxdrEnum();
                c.set_min_max_val(2);
                codeLength += c.encode(output);
                return codeLength;
            }else{
                throw new Error("symmetrySecurity is null");
            }
        }else if (this.choice == Choice.PASSWORD_SECURITY) {
            if(this.passwordSecurity != null){
                codeLength += this.passwordSecurity.encode(output);
                let c = new AxdrEnum();
                c.set_min_max_val(1);
                codeLength += c.encode(output);
                return codeLength;
            }else{
                throw new Error("passwordSecurity is null");
            }
        }else if (this.choice == Choice.NULL_SECURITY) {
            if(this.nullSecurity != null){
                codeLength += this.nullSecurity.encode(output);
                let c = new AxdrEnum();
                c.set_min_max_val(0);
                codeLength += c.encode(output);
                return codeLength;
            }else{
                throw new Error("nullSecurity is null");
            }
        }
        throw new Error("Error encoding AxdrChoice: No item in choice was encoded.");
    }

    getChoiceIndex(): Choice {
        return this.choice;
    }

    resetChoices() {
        this.choice = Choice._ERR_NONE_SELECTED;
        this.nullSecurity = null;
        this.passwordSecurity = null;
        this.symmetrySecurity = null;
        this.signatureSecurity = null;
    }

    encodeAndSave(encodingSizeGuess: number) {
        let revOStream = new ReverseByteArrayOutputStream();
        revOStream.setBufSize(encodingSizeGuess);
        this.encode(revOStream);
        this.dataCode = revOStream.getArray();
    }

    toString(): string {
        if (this.choice == Choice.NULL_SECURITY) {
            if (this.nullSecurity != null) {
                return "choice: {nullSecurity: " + this.nullSecurity + "}";
            } else {
                return "choice is nullSecurity but nullSecurity is null";
            }
        }else if(this.choice == Choice.PASSWORD_SECURITY){
            if(this.passwordSecurity != null){
                return "choice: {passwordSecurity: " + this.passwordSecurity + "}";
            }else{
                return "choice is passwordSecurity but passwordSecurity is null";
            }
        }else if(this.choice == Choice.SYMMETRY_SECURITY){
            if(this.symmetrySecurity != null){
                return "choice: {symmetrySecurity: " + this.symmetrySecurity + "}";
            }else{
                return "choice is symmetrySecurity but symmetrySecurity is null";
            }
        } else if(this.choice == Choice.SIGNATURE_SECURITY){
            if(this.signatureSecurity != null){
                return "choice: {signatureSecurity: " + this.signatureSecurity + "}";
            }else{
                return "choice is signatureSecurity but signatureSecurity is null";
            }
        } else {
            return "unknown";
        }
    }



}
